#!/bin/bash

# Prints the subprojects in `projects` that use the specified project.

set -e

PROJECTNAME=$1
PROJECT=""

while read line
do
  THISNAME=`basename $line`
  if [ $PROJECTNAME == $THISNAME ]  || [ $PROJECTNAME == $line ]; then
    PROJECT=$line
  fi
done < projects

if [ -z $PROJECT ]; then
  echo "Must specify project"
  exit 1
fi

#echo "Finding who uses $PROJECT"

check_downstream() {
  NAME=`basename $1`
  cd $NAME
  # allow grep to return an error
  USESVERSION=$(clj -Stree | grep "$PROJECT" || true)
  if [[ $USESVERSION != "" ]]; then
    echo $NAME depends on $USESVERSION
  fi
  cd ..
}

export -f check_downstream
export PROJECT
cat projects | parallel check_downstream {}
